//
//  LBHomeFunctionListView.m
//  爱鲜蜂
//
//  Created by 李田 on 16/11/8.
//  Copyright © 2016年 tianyao. All rights reserved.
//

#import "LBHomeFunctionListView.h"
#import "LBHomeFunctionFlowLayout.h"
#import "LBHomeFunctionFirstCell.h"
#import "LBHomeFunctionScollCell.h"
#import "LBHomeFunctionDimeStoreCell.h"
#import "LBHomeFunctionCategoryCell.h"
#import "LBGorenjeCell.h"
#import "LBCategoryFoodCell.h"
#import "LBNavBarView.h"
#import "LBActRowsModel.h"
#import "LBActivityModel.h"
#import "LBCategoryDetailModel.h"
@interface LBHomeFunctionListView ()<UICollectionViewDataSource, UICollectionViewDelegate, LBCategoryFoodCellDelegate, CAAnimationDelegate, LBHomeFunctionCategoryCellDelegate>

//upBtn
@property(nonatomic, weak)UIButton *upBtn;
@property(nonatomic)BOOL num1;
@property(nonatomic)BOOL num2;
@property(nonatomic, assign)NSInteger section;
@end

static NSString *cellID = @"cell_ID";
static NSString *topCellID = @"topCell_ID";
static NSString *scrollCellID = @"scrollCell_ID";
static NSString *dimeStoreCellID = @"dimeStoreCell_ID";
static NSString *categoryCellID = @"categoryCell_ID";
static NSString *gorenjeCellID = @"gorenjeCell_ID";
static NSString *categoryFoodCellID = @"categoryFoodCell_ID";
@implementation LBHomeFunctionListView{
    
    CGFloat locY;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        //        NSLog(@"%@",_categoryModelList);
        
    }
    return self;
}
#pragma mark - 代理 传indexPath.section
- (void)homeFunctionCategoryCell:(LBHomeFunctionCategoryCell *)homeFunctionCategoryCell{
    
    //拿到model数组
    LBActInfoModel *acinfo = _actInfoModelArray[5];
    //遍历判断获取行号
    for (NSInteger i = 0; i < acinfo.act_rows.count; i++) {
        if ([homeFunctionCategoryCell.model.activity.name isEqualToString:acinfo.act_rows[i].activity.name]) {
            self.section = i + 3;
            break;
        }
    }
    //行号修正
    switch (self.section) {
        case 3:
        case 4:
            break;
        case 5:
        case 6:
            self.section = self.section + 1;
            break;
            
        default:
            break;
    }
    
    if ([_delegate respondsToSelector:@selector(homeFunctionListView:indexPathSection:)]) {
        
        [_delegate homeFunctionListView:self indexPathSection:self.section];
    }
}

#pragma mark - 实现接收startP 实现代理方法
- (void)categoryFoodCell:(LBCategoryFoodCell *)categoryFoodCell startPoint:(CGPoint)startP urlStr:(NSString *)urlStr{
    
    [self addAnimationWithStartPoint:startP urlStr:urlStr];
}

//    MARK:添加动画
- (void)addAnimationWithStartPoint: (CGPoint)startP urlStr: (NSString *)urlStr{
    
    //    MARK:1.0 添加小红点
    UIImageView *redImageView = [[UIImageView alloc] init];
    
    [redImageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    [redImageView sizeToFit];
    //    MARK:2.0 设置位置
    redImageView.center = startP;
    
    //    MARK:3.0 添加keyWindow
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    //    MARK:4.0 把小红点添加到keyWindow上面
    [keyWindow addSubview:redImageView];
    
    //    MARK:5.0 创建关键帧动画
    CAKeyframeAnimation *keyAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //创建贝塞尔曲线
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //起点
    [path moveToPoint:startP];
    
    //最高点
    CGPoint controlP = CGPointMake(startP.x, startP.y);
    
    //终点
    CGPoint endP = CGPointMake(235, kScreenHeight - 46);
    
    //设置路径的 最高点 和 终点
    //参数1:终点
    //参数2:最高点
    [path addQuadCurveToPoint:endP controlPoint:controlP];
    
    //给动画添加路径
    keyAni.path = path.CGPath;
    
    //设置动画时长
    keyAni.duration = 0.75;
    
    //设置代理监听小红点结束  --- 删除小红点
    keyAni.delegate = self;
    
    //缩放
    [UIView animateWithDuration:0.75 animations:^{
        
        redImageView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        redImageView.alpha = 0.2;
    }];
    
    //动画结束的时候删除小红点 --- 注意 在添加小红点之前删除
    // KVC 这里是核心动画独有的
    [keyAni setValue:redImageView forKey:@"red"];
    
    //添加动画到小红点
    [redImageView.layer addAnimation:keyAni forKey:@"keyAni"];
    
}

//    MARK: 动画结束
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    // 取出你关联的小红点
    UIImageView *redImageView = [anim valueForKey:@"red"];
    
    // 将小红点从父控件删除
    [redImageView removeFromSuperview];
    
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    locY = scrollView.contentOffset.y;


    
    
    
}

#pragma mark - 代理 监听滚动结束 获取偏移量
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offsetY = scrollView.contentOffset.y+ 20;
    
    
    if (offsetY > kScreenHeight && scrollView.contentOffset.y -  locY < 0) {
        self.upBtn.hidden = NO;
    }else if(offsetY == 1925.5){
        self.upBtn.hidden = NO;
    }else{
        self.upBtn.hidden = YES;
    }

    NSDictionary *dic  = @{@"offsetY":@(offsetY)};

    [[NSNotificationCenter defaultCenter]postNotificationName:@"offset" object:self userInfo:dic];
    
}

#pragma mark - 1.0 创建collectionV
- (void)setupUI{
    
    _num1 = YES;
    _num2 = YES;
    LBHomeFunctionFlowLayout *flowLayout = [[LBHomeFunctionFlowLayout alloc] init];
    
    UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    
    //    MARK:设置背景颜色
    collectionV.backgroundColor = [UIColor cz_colorWithHex:0xf5f5f9];
    [self addSubview:collectionV];
    
    //    MARK: 设置向上滚动的btn箭头
    UIButton *upBtn = [[UIButton alloc] init];
    [upBtn setImage:[UIImage imageNamed:@"scroll_to_top"] forState:UIControlStateNormal];
    upBtn.hidden = YES;
    
    [upBtn addTarget:self action:@selector(scrollUpBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:upBtn];
    


    //    //    MARK: 创建假导航条view
    //    LBNavBarView *navV = [[LBNavBarView alloc] init];
    //    navV.backgroundColor = [UIColor orangeColor];
    //    navV.alpha = 0.2;
    //    [self.view addSubview:navV];
    //
    //    [navV mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.right.top.equalTo(self.view);
    //        make.height.mas_equalTo(64);
    //    }];
    

    //    MARK:代理
    collectionV.dataSource = self;
    
    collectionV.delegate = self;
    
    //    MARK: 注册
    [collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    
    [collectionV registerClass:[LBHomeFunctionFirstCell class] forCellWithReuseIdentifier:topCellID];
    
    [collectionV registerClass:[LBHomeFunctionScollCell class] forCellWithReuseIdentifier:scrollCellID];
    
    [collectionV registerClass:[LBHomeFunctionDimeStoreCell class] forCellWithReuseIdentifier:dimeStoreCellID];
    
    [collectionV registerClass:[LBHomeFunctionCategoryCell class] forCellWithReuseIdentifier:categoryCellID];
    
    [collectionV registerClass:[ LBGorenjeCell class] forCellWithReuseIdentifier:gorenjeCellID];
    
    [collectionV registerClass:[LBCategoryFoodCell class] forCellWithReuseIdentifier:categoryFoodCellID];
    
    //    MARK: 关联
    self.collectionV = collectionV;
    self.upBtn = upBtn;
    //    MARK: 设置回到顶部的btn
    [collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.mas_equalTo(-20);
    }];
    
    [upBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-24);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.bottom.equalTo(self).offset(-23 - kNaviHeight);
    }];
    
    
}

#pragma mark - 3.0 向上滚动的点击事件
- (void)scrollUpBtnClick: (UIButton *)upBtn{
    
    [self.collectionV scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}

#pragma mark - 2.0 设置数据源
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 7;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0 || section == 1) {
        return 1;
    }else if(section == 2){
        return 5;
    }else{
        return 4;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //    NSString *identifier = cellID;
    if (indexPath.section == 0) {
        
        //        identifier = topCellID;
        
        LBHomeFunctionFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:topCellID forIndexPath:indexPath];
        //轮播器
        LBActInfoModel *acinfo = _actInfoModelArray[0];
        NSMutableArray *imageArray = [NSMutableArray array];
        for(NSInteger i = 0; i < acinfo.act_rows.count; i++){
            LBActRowsModel *actRows = acinfo.act_rows[i];
            [imageArray addObject:actRows.activity.img];
        }
        cell.imageArr = imageArray.copy;
        //每日签到
        LBActInfoModel *signInModel = _actInfoModelArray[1];
        cell.imageDataArray =  signInModel.act_rows;
        return cell;
    }else if (indexPath.section == 1){
        
        
        LBHomeFunctionScollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:scrollCellID forIndexPath:indexPath];
        LBActInfoModel *acinfo = _actInfoModelArray[3];
        NSMutableArray *imageArray = [NSMutableArray array];
        for(NSInteger i = 0; i < acinfo.act_rows.count; i++){
            LBActRowsModel *actRows = acinfo.act_rows[i];
            [imageArray addObject:actRows.activity.img];
        }
        cell.imageArr = imageArray.copy;
        return cell;
    }
    if (indexPath.section == 2 && indexPath.row == 0){
        
        LBHomeFunctionDimeStoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:dimeStoreCellID forIndexPath:indexPath];
        
        return cell;
    }
    
    if(indexPath.section == 2 && indexPath.row != 0){
        
        LBGorenjeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:gorenjeCellID forIndexPath:indexPath];
        
        if (_num1 &&(indexPath.item == 1) ) {
            self.num1 = NO;
            cell.imgModel = self.imgDataList[indexPath.item - 1];
            return cell;
        }else if(_num2 &&(indexPath.item == 2)){
            self.num2 = NO;
            cell.imgModel = self.imgDataList[indexPath.item - 1];
            return cell;
        }else{
            cell.imgModel = self.imgDataList[indexPath.item -1];
            return cell;
        }
        
    }
    if(indexPath.section > 2 && indexPath.row == 0){
        
        LBHomeFunctionCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:categoryCellID forIndexPath:indexPath];
        LBActInfoModel *acinfo = _actInfoModelArray[5];
        
        LBActRowsModel *actRow = acinfo.act_rows[indexPath.section -3];
        cell.model = actRow;
        ((LBHomeFunctionCategoryCell *)cell).delegate = self;
        //        self.section = indexPath.section;
        return cell;
    }else{
        
        LBCategoryFoodCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:categoryFoodCellID forIndexPath:indexPath];
        
        LBActInfoModel *acinfo = _actInfoModelArray[5];
        LBActRowsModel *actRow = acinfo.act_rows[indexPath.section -3];
        LBCategoryDetailModel *categoryDetail = actRow.category_detail;
        LBGoodsModel *goodsModel = categoryDetail.goods[indexPath.row -1];
        cell.goodModel = goodsModel;
        
        ((LBCategoryFoodCell *)cell).delegate = self;
        return cell;
        
    }
}
-(void)setActInfoModelArray:(NSMutableArray *)actInfoModelArray{
    _actInfoModelArray = actInfoModelArray;
    //轮播器模型
    [self.collectionV reloadData];
}

#pragma mark - 设置每一组和组中不同item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat width = kScreenWidth;
    
    if (indexPath.section == 0) {
        CGFloat height = 240;
        
        return CGSizeMake(width, height);
    }else if(indexPath.section == 1){
        
        return CGSizeMake(width, 140);
    }else if(indexPath.section == 2 && indexPath.row == 0){
        
        return CGSizeMake(width, 220);
    }else if(indexPath.section == 2 && indexPath.row != 0){
        
        CGFloat width = (kScreenWidth - 1) / 2;
        return CGSizeMake(width, 80);
    }else if((indexPath.section == 3 && indexPath.row == 0) || (indexPath.section == 4 && indexPath.row == 0) || (indexPath.section == 5 && indexPath.row == 0) || (indexPath.section == 6 && indexPath.row == 0) || (indexPath.section == 7 && indexPath.row == 0)){
        
        return CGSizeMake(width, 140);
    }else if(indexPath.section == 3 && indexPath.row != 0){
        
        return CGSizeMake(124, 200);
    }else{
        return CGSizeMake(124, 200);
    }
    
}



@end




